<?php

namespace app\api\controller\store;

use app\admin\model\order\Order as OrderModel;
use app\api\controller\Base;
use app\common\service\lib\QrcodeImg;
use app\common\service\OrderService;
use app\common\service\store\PinkService;
use app\Request;
use app\validate\PinkCheck;
use app\admin\model\store\Pink as PinkModel;

class Pink extends Base
{
    public function message($data = [])
    {
        return $data = ['code' => 200, 'message' => '成功', 'data' => $data];
    }

    //前台拼团列表接口
    public function pinkList()
    {
        $res = PinkService::pinkListInfo();
        return $this->res($res);
    }

    //拼团活动详情接口
    public function pinkDesc()
    {
        $pinkId = request()->get('pink_id');
        $data = PinkService::pinkDesc($pinkId);
        return $this->res($data);
    }

    //前台用户正在参与拼团接口
    public function userInPink(Request $request)
    {
        $userId = $request->uid;
        $where [] = ['user_id', '=', $userId];
        $where [] = ['pink_id', '>', 0];
        $where [] = ['pink_no', '<>', 0];
        $feild = ['pink_no', 'pink_id', 'is_pink_master', 'is_pink_status'];
        $data = OrderModel::where('user_id', $userId)
            ->where($where)
            ->field($feild)
            ->with('pinkConfig')
            ->select();
        $data && $data = $data->toArray();
        foreach ($data as &$val) {
            $val['end_time'] = date('H:i:s', $val['end_time']);
        }
        unset($val);
        $data = ['code' => 200, 'message' => '成功', 'data' => $data];
        return $this->res($data);
    }

    //前台用户参与拼团商品记录
    public function joinDesc()
    {
        $pinkNo = request()->get('pinkNo');
        $pinkInfo = PinkModel::where('pink_no', $pinkNo)
            ->where('status', '1')
            ->with('pinkConfig')
            ->withoutField(['update_time', 'delete_time'])
            ->find();
        $pinkInfo && $pinkInfo = $pinkInfo->toArray();
        $data = $this->message($pinkInfo);
        return json($data);
    }

    //前台用户拼团分享接口
    public function invitation()
    {
        $pinkNo = request()->get('pinkNo');
        $qrcode = new QrcodeImg();
        $url = "local.ea.com/api/pink/Desc";
        $url = $qrcode->index($url);
        $data = $this->message($url);
        return json($data);
    }

    //用户拼团记录
    public function log()
    {
        $pinkNo = request()->get('pinkNo');
        $data = OrderModel::where('pink_no', $pinkNo)->field('user_id')->with('user')->select();
        $data && $data = $data->toArray();
        $data = $this->message($data);
        return json($data);
    }

    /**用户开团，参团接口
     * Author:William
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function pinkOpen(Request $request)
    {
        //拼团活动参数
        $post = request()->post();
        $order['user_id'] = $request->uid;
        $this->validateFail(PinkCheck::class, $post);
        //檢測庫存
        $res = OrderService::goodsSkuSumCheck($post);
        if ($res['code'] == 400) {
            return $this->res($res);
        }
        //訂單編號爲空
        if (empty($post['pink_no'])) {
            //调用创建拼团方法
            $res = PinkService::pinkCreate($post);
            return $this->res($res);
        } else {
            //调用参与拼团方法
            $res = PinkService::joinPink($post);
            return $this->res($res);
        }
        return $this->res($res);
    }
}